<template>
  <div class="AdendDialog">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="22">
          <el-form-item label="部门名称" prop="fullName">
            <el-input v-model="ruleForm.fullName" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="上级部门" prop="pid">
            <el-select v-model="ruleForm.pid" placeholder="请选择上级部门">
              <el-option label="顶级" :value="0" />
              <el-option v-for="(item,index) in orgList" :key="index" :label="item.fullName" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="备注">
            <el-input v-model="ruleForm.remark" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    props: ['row'],
    data() {
      return {
        ruleForm: {
          id: null,
          fullName: '',
          pid: '',
          remark: '',
        },
        rules: {
          fullName: [{
            required: true,
            message: '请输入部门名称',
            trigger: 'blur'
          }],
          pid: [{
            required: true,
            message: '请选择上级部门',
            trigger: 'change'
          }],
        },

        orgList: [],
      }
    },
    created: function() {
      this.ruleForm = {
        id: this.row.id,
        fullName: this.row.fullName,
        pid: this.row.pid,
        remark: this.row.remark,
      }
      // 获取部门选择列表
      this.getSysOrgSelectList();
    },
    methods: {
      // 获取部门选择列表
      getSysOrgSelectList: function() {
        let params = {};
        this.$server.getSysOrgSelectList(params).then(res => {
          this.orgList = res.orgList;
        })
      },
      // 表单提交
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let params = this.ruleForm;
            this.$server.updateSysOrg(params).then(res => {
              this.$message({
                type: "success",
                message: "修改成功!"
              });
              // 关闭弹框
              this.$emit('chang');
            })
          } else {
            // console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$emit('chang');
      },
    }
  }
</script>
<style lang="scss" scoped>
  .AdendDialog {

    .el-date-editor.el-input,
    .el-date-editor.el-input__inner,
    .el-input,
    .el-select,
    .el-input__inner {
      width: 100%;
    }
  }

  .el-cascader {
    width: 100%;
  }
</style>
